/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   01.03.2010
 * @version SVN: $Id: StockItemRepositoryImpl.java 159 2010-05-19 15:27:33Z christian-kerl@web.de $
 */

package org.cakis.server.model.repository.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import org.cakis.server.Names;
import org.cakis.server.model.StockItem;
import org.cakis.server.model.repository.Range;
import org.cakis.server.model.repository.StockItemRepository;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Name;

/**
 * Implementation of the {@link StockItemRepository} interface.
 * 
 * @author Christian Kerl
 */
@Name(Names.StockItemRepository)
@AutoCreate
@Stateless(name=Names.StockItemRepository)
public class StockItemRepositoryImpl extends AbstractRepository<StockItem> implements StockItemRepository {
    private static final long serialVersionUID = 1L;

    @Override
    protected Long getId(StockItem object) {
        return object.getId();
    }

    @Override
    public List<StockItem> findByProduct(Long productId, Range limit) {
        Query q = em.createQuery("select s from StockItem s where s.product.id = :product");
        q.setParameter("product", productId);

        return list(limit(q, limit));
    }

    @Override
    public List<StockItem> findByProductOrProductType(Long baseProductId) {
        Query q = em.createQuery("select s from StockItem s where s.product.id = :id or s.product.type.id = :id");
        q.setParameter("id", baseProductId);

        return list(q);
    }
}
